package com.car.controller;


import com.car.service.UserService;
import com.car.pojo.EmpQueryParam;
import com.car.pojo.PageResult;
import com.car.pojo.Result;
import com.car.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器
 */
@Slf4j
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 分页查询用户
     */
    @GetMapping("/users")
    public Result page(EmpQueryParam param) {
        log.info("分页查询用户: {}", param);
        PageResult<User> pageResult = userService.page(param);
        return Result.success(pageResult);
    }
    /**
     * 新增用户
     */
    @PostMapping("/users")
    public Result save(@RequestBody User user) throws Exception {
        log.info("新增用户: {}", user);
        userService.save(user);
        return Result.success();
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/users")
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("删除用户: {}", ids);
        userService.delete(ids);
        return Result.success();
    }

    /**
     * 根据ID查询用户信息
     */
    @GetMapping("/users/{id}")
    public Result getInfo(@PathVariable Integer id) {
        log.info("根据ID查询用户信息: {}", id);
        User user = userService.getInfo(id);
        return Result.success(user);
    }

    /**
     * 修改用户
     */
    @PutMapping("/users")
    public Result update(@RequestBody User user) {
        log.info("修改用户: {}", user);
        userService.update(user);
        return Result.success();
    }

    /**
     * 查询所有用户
     */
    @GetMapping("/users/list")
    public Result list() {
        log.info("查询所有用户");
        List<User> userList = userService.list();
        return Result.success(userList);
    }
    
}